package bearworker.zxl.claw;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 逍遥的熊 on 2018/1/28.
 */
public class PcaCodeClaw {

    public List<CodeMap> pcaClaw(){
        File file = new File("src/main/resource/pca-code.json");
        Reader reader = null;
        Gson gson = new Gson();
        List<CodeMap> codeMapList = new ArrayList<>();
        try{
            reader = new InputStreamReader(new FileInputStream(file));
            int tempbyte;
            StringBuffer codeStr = new StringBuffer();
            while ((tempbyte = reader.read()) != -1) {
                if (((char) tempbyte) != '\r') {
                    codeStr.append((char) tempbyte);
                }
            }
            reader.close();
            System.out.println(codeStr.toString());
            ArrayList<Object> list = gson.fromJson(codeStr.toString(), ArrayList.class);
            for(Object a : list){
                CodeMap cmap = new CodeMap();
                HashMap<String,Object> map = gson.fromJson(a.toString(),HashMap.class);

                cmap.setCode(map.get("code").toString());
                cmap.setName(map.get("name").toString());
                String cChilds = map.get("childs").toString();
                ArrayList<Object> clist = gson.fromJson(cChilds,ArrayList.class);
                List<CCodeMap> cclist = new ArrayList<>();

                for(Object c : clist){
                    CCodeMap cCodeMap = new CCodeMap();
                    HashMap<String,Object> ccmap = gson.fromJson(c.toString(),HashMap.class);
                    cCodeMap.setCode(ccmap.get("code").toString());
                    cCodeMap.setName(ccmap.get("name").toString());

                    String dChilds = ccmap.get("childs").toString();

                    ArrayList<Object> dclist = gson.fromJson(dChilds,ArrayList.class);
                    List<DCodeMap> ddlist = new ArrayList<>();

                    for(Object d : dclist){
                        DCodeMap dCodeMap = new DCodeMap();

                        HashMap<String,Object> dcmap = gson.fromJson(d.toString(),HashMap.class);

                        dCodeMap.setCode(dcmap.get("code").toString());
                        dCodeMap.setName(dcmap.get("name").toString());

                        ddlist.add(dCodeMap);
                    }
                    cCodeMap.setDCodeMapList(ddlist);
                    cclist.add(cCodeMap);
                }
                cmap.setcCodeMapList(cclist);

                codeMapList.add(cmap);
            }

        }catch (Exception e){
            System.out.println(e);
        }

        return codeMapList;
    }

    public class CodeMap{
        String code;
        String name;
        List<CCodeMap> cCodeMapList;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<CCodeMap> getcCodeMapList() {
            return cCodeMapList;
        }

        public void setcCodeMapList(List<CCodeMap> cCodeMapList) {
            this.cCodeMapList = cCodeMapList;
        }
    }

    public class CCodeMap{
        String code;
        String name;
        List<DCodeMap> DCodeMapList;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<DCodeMap> getDCodeMapList() {
            return DCodeMapList;
        }

        public void setDCodeMapList(List<DCodeMap> DCodeMapList) {
            this.DCodeMapList = DCodeMapList;
        }
    }

    public class DCodeMap{
        String code;
        String name;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
